package server.io.block;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import server.io.client.HandShake;
import server.io.pro.ctrl.Pro_Adapter_Recv;

public class BlockServer
{
	
	public static void main(String[] args) throws IOException
	{
		ServerSocket server = new ServerSocket(8555);
		Socket client = server.accept();
		BufferedReader in = new BufferedReader(new InputStreamReader(
				client.getInputStream(), "utf-8"));
		PrintWriter out = new PrintWriter(client.getOutputStream());
		while (true)
		{
			System.out.println("Waiting..!");
			HandShake hand = new HandShake();
			Pro_Adapter_Recv proadp = new Pro_Adapter_Recv();
			
			String str = "";
			int size = 0;
			char[] cbuf = new char[1024];
			while ((size = in.read(cbuf)) != -1)
			{
				System.out.println("read:" + cbuf.toString() + " size:" + size);
				
				str = new String(cbuf);
				
				System.out.println("_str:\n" + str);
				
				int result = proadp.parseHead(str);
				
				if(result == 11)
				{
					byte[] pro = hand.createProtocal(str);
					
					String _handpro = new String(pro);
					
					System.out.println("pro:\n" + _handpro);
					out.println(_handpro);
					out.flush();
				}
				else
				{
					System.out.println("Recv!!");
				}

				
				if (str.equals("end"))
					break;
			}
			
			System.out.println("Close!");
			
			client.close();
			
			break;
		}
		

	}
}
